//
//  TSTClockPage.m
//  TST
//
//  Created by POP3mac on 2020/4/27.
//  Copyright © 2020 YTYanK. All rights reserved.
//

#import "TSTClockPage.h"

@interface TSTClockPage ()
@property (strong, nonatomic) UIButton * clockBtn;
@end

@implementation TSTClockPage

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
      [self setAllInitSubView];
      [self setAllSubViewLayout];
      [self updateAllData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


- (void)setAllInitSubView {
    self.clockBtn = [[UIButton alloc] initWithFrame:CGRectZero];
//    self.clockBtn.backgroundColor = YTYRGB(0, 107, 95);
    CGFloat  w = SCREEN_WIDTH * 0.3;
    [self.clockBtn setViewBorderCornerRadius:w/2 borderWidth:1 borderColor:UIColor.clearColor];
    [self.clockBtn addTarget:self action:@selector(clockBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.clockBtn];
    
  
}

- (void)setAllSubViewLayout {
    
    CGFloat  w = SCREEN_WIDTH * 0.3;
    [self.clockBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view);
        make.height.mas_equalTo(w);
        make.width.mas_equalTo(w);
    }];
}

- (void)updateAllData {
    [self.clockBtn setTitle:@"打卡" forState:UIControlStateNormal];
}


- (void)clockBtn:(UIButton *)sender {
    NSLog(@"打卡??");
    if (self.lat < 1 && self.lon < 1) {
        [MBProgressHUD showError:@"無法獲取經緯度，無法進行打卡"];
    }else {
        [TSTApi requestWithUrl:AUOnClock parameters:@{@"staff_id":[TSTApi sharedTSTApi].userModel.staff_id,@"lnt":@(self.lon),@"lat":@(self.lat)} method:NetMethodPOST returnModel:^(id  _Nonnull model) {
            [MBProgressHUD showSuccess:@"打卡成功"];

            self.clockBtn.backgroundColor = UIColor.grayColor;
          
            self.clockBtn.enabled = YES;
        } returnError:^(id  _Nonnull error) {
            [MBProgressHUD showError:error];
        }];
    }
}

- (void)setIsClock:(BOOL)isClock {
    _isClock =isClock;
    NSLog(@"打卡?? %d", _isClock);
   // is_clock = 1 == 打卡了, 0 == 还没有打卡 = false
    if (_isClock) {
        self.clockBtn.backgroundColor = UIColor.grayColor;
         self.clockBtn.enabled = YES;
    }else {
        self.clockBtn.backgroundColor = YTYRGB(0, 107, 95);
         self.clockBtn.enabled = NO;
    }
    
//    [self.clockBtn setEnabled:!_isClock];
}


@end
